package design.pattern.Lu16ObserverPattern;

import lombok.Data;

import java.util.concurrent.TimeUnit;

/**
 * @program JavaBase
 * @description: 观察者模式，没有效率的
 * @author: zhanglu
 * @create: 2020-04-10 09:41:00
 */
public class ObserverPattern01 {

    public static void main(String[] args) throws InterruptedException {

        HanFeiZi01 hanFeiZi01 = new HanFeiZi01();
        LiSi01 liSi01 = new LiSi01();

        Runnable watch01 = () -> {
            while (true){
                if(hanFeiZi01.isHaveBreakfast()){
                    liSi01.update("");
                    hanFeiZi01.setHaveBreakfast(false);
                }
            }
        };
        Runnable watch02 = () -> {
            while (true){
                if(hanFeiZi01.isHaveFun()){
                    liSi01.update("");
                    hanFeiZi01.setHaveFun(false);
                }
            }
        };
        new Thread(watch01).start();
        new Thread(watch02).start();

        hanFeiZi01.haveBreakfast();
        TimeUnit.SECONDS.sleep(2);
        hanFeiZi01.haveFun();


    }

}

interface IHanFeiZi01{
    void haveBreakfast();
    void haveFun();
}

interface ILiSi01{
    void update(String str);
}

@Data
class HanFeiZi01 implements IHanFeiZi01{

    private boolean isHaveBreakfast;
    private boolean isHaveFun;

    @Override
    public void haveBreakfast() {
        System.out.println("我吃饭了");
        this.isHaveBreakfast = true;
    }

    @Override
    public void haveFun() {
        System.out.println("我娱乐了");
        this.isHaveFun = true;
    }

}

class LiSi01 implements ILiSi01{

    @Override
    public void update(String str) {
        System.out.println("监控到他的动作：" + str);
    }
}

